/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.base.fs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author kostya
 */
public class FileManager {

    public static final PlainFileType PLAIN_FILE_TYPE = new PlainFileType();

    private static Map<String, FileType> fileTypes = new HashMap<String, FileType>();
    public static String FILE_STORAGE_PATH = "";

    public static void setFileType(FileType aFileType){
        fileTypes.put(aFileType.getCode(), aFileType);
    }

    public static FileType getFileType(String aCode){
        return fileTypes.get(aCode);
    }

    public static Set<String> getFileTypes(){
        return fileTypes.keySet();
    }

    public static String[] getPlainFileTypes(){
        List<String> result = new ArrayList<String>();
        for (String string : fileTypes.keySet()) {
            if(getFileType(string).isPlainFile())
                result.add(string);
        }
        return result.toArray(new String[0]);
    }

    public static void checkFileDescriptorName(FileDescriptor aDescriptor)throws FileException{
        if(aDescriptor.getName()==null || "".equals(aDescriptor.getName()))
            throw new FileException("File name can't be empty");
        if(aDescriptor.getName().indexOf("/")!=-1 || aDescriptor.getName().indexOf("\\")!=-1)
            throw new FileException("File name contains incorrect characters");
        if(aDescriptor.getParentFile()==null)
            throw new FileException("Can't find parent file or folder");
//        FileDescriptor[] fds = aDescriptor.getParentFile().getProvider().listFiles(aDescriptor.getParentFile());
//        for (FileDescriptor ch : fds) {
//            if(aDescriptor.getId()==null){
//                if(ch.getName().equalsIgnoreCase(aDescriptor.getName()))
//                    throw new FileException("Can't create file under name "+
//                            aDescriptor.getName()+". Another file is already exists");
//            }else{
//                if(ch.getName().equalsIgnoreCase(aDescriptor.getName()) && !ch.getId().equals(aDescriptor.getId()))
//                    throw new FileException("Can't save file under name "+
//                            aDescriptor.getName()+". Another file is already exists");
//            }
//        }
    }
}
